{% extends 'layout.html' %}
{% block content %}
    <div class="container">
        <div style="margin-bottom: 14px">
            <input type="button" value="新建任务" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
            <input id="btnAdd" type="button" value="新建任务" class="btn btn-primary">
        </div>

        <div class="panel panel-default">
            <!-- Default panel contents -->
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                任务列表
            </div>
            <table class="table table-bordered">
                <thead>

                <tr>
                    <th>ID</th>
                    <th>任务创建时间</th>
                    <th>任务标题</th>
                    <th>参与人数</th>
                    <th>状态</th>
                    <th>负责人</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for obj in queryset %}
                    <tr>
                        {#                    {% for obj in queryset %}#}
                        <th>{{ obj.id }}</th>
                        <td>{{ obj.oid }}</td>
                        <td>{{ obj.title }}</td>
                        <td>{{ obj.price }}</td>
                        <td>{{ obj.get_status_display }}</td>
                        <td>{{ obj.admin.username }}</td>
                        <td>
                            <input uid="{{ obj.id }}" class="btn btn-primary btn-xs btn-edit" type="button"
                                   value="编 辑">
                            <input uid="{{ obj.id }}" class="btn btn-danger btn-xs btn-delete" type="button"
                                   value="删 除">
                        </td>

                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    {# #添加订单对话框#}
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新建</h4>
                </div>
                <div class="modal-body">
                    <form id="formAdd">
                        <div class="clearfix">
                            {% for field in form %}
                                <div class="col-lg-6">
                                    <div class="form-group" style="position: relative;">
                                        <label>{{ field.label }}</label>
                                        {{ field }}
                                        <span class="errors_msg" style="color: #b92c28;position: absolute"></span>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                    <button id="btnSave" type="button" class="btn btn-primary">保 存</button>
                </div>
            </div>
        </div>
    </div>
    {##删除对话框#}
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                    <h4>提示信息</h4>
                    <p style="margin-top: 12px">点击确认后将会删除所有的信息，是否确认？</p>
                    <p style="text-align: right">
                        <button id="btnDeleteConfirm" type="button" class="btn btn-danger">确 认</button>
                        <button type="button" class="btn btn-default " data-dismiss="modal">取 消</button>
                    </p>
                </div>
            </div>
        </div>
    </div>


{% endblock %}


{% block js %}
    <script type="text/javascript">
        var DELETE_ID;
        var EDIT_ID;
        $(function () {
            bindBtnAddEvent();
            bindBtnSaveEvent();
            bindBtnDeleteEvent();
            bindBtnDeleteConfirmEvent();
            bindBtnEditEvent();
        })

        //这个js的格式明显比前边的格式严格的多
        function bindBtnAddEvent() {
            $("#btnAdd").click(function () {
                //将正在编辑的ID设置为空
                EDIT_ID = undefined;
                $("#formAdd")[0].reset();
                $(".modal-title").text("新建");
                $('#myModal').modal('show');
            });
        }

        function bindBtnSaveEvent() {
            $("#btnSave").click(function () {
                $(".errors_msg").empty();
                if (EDIT_ID) {
                    //编辑
                    doEdit();
                } else {
                    doAdd();
                }

            });
        }

        function doAdd() {
            $.ajax({
                url: "/order/add/",
                type: "post",
                data: $("#formAdd").serialize(),
                dataType: "JSON",
                success: function (res) {
                    {#console.log(res);#}
                    if (res.status) {
                        alert("创建成功");
                        location.reload();
                        $("#formAdd")[0].reset();
                        $('#myModal').modal('hide');


                    } else {
                        $.each(res.error, function (name, errorList) {
                            $("#id_" + name).next().text(errorList[0]);
                        },)
                    }
                }
            })
        }

        function doEdit() {
            $.ajax({
                url: "/order/edit/" + "?uid=" + EDIT_ID,
                type: "post",
                data: $("#formAdd").serialize(),
                dataType: "JSON",
                success: function (res) {
                    {#console.log(res);#}
                    if (res.status) {
                        alert("创建成功");

                        $("#formAdd")[0].reset();
                        $('#myModal').modal('hide');
                        location.reload();


                    } else {
                        if (res.tips) {
                            alert(res.tips);
                        } else {
                            $.each(res.error, function (name, errorList) {
                                $("#id_" + name).next().text(errorList[0]);
                            })
                        }

                    }
                }
            })
        }

        function bindBtnDeleteEvent() {
            $(".btn-delete").click(function () {
                {#alert("点击了")#}
                $("#deleteModal").modal('show');
                DELETE_ID = $(this).attr("uid");
            });
        }

        //确认删除的按钮函数控制
        function bindBtnDeleteConfirmEvent() {
            $("#btnDeleteConfirm").click(function () {
                $.ajax({
                    url: "/order/delete/",
                    type: "GET",
                    data: {
                        uid: DELETE_ID
                    },
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            location.reload();
                            //其实还有方法就是去使用js的前台页面，将
                            //整行的数据来进行删除也是可以的
                        } else {
                            alert(res);
                        }
                    },
                })
            })
        }

        //进行数据的编辑
        function bindBtnEditEvent() {
            $(".btn-edit").click(function () {
                $("#formAdd")[0].reset();
                var uid = $(this).attr("uid");
                EDIT_ID = uid;
                $.ajax({
                    url: "/order/detail/",
                    type: "GET",
                    data: {
                        uid: uid
                    },
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            {#console.log(res);#}
                            $(".modal-title").text("编辑");
                            $('#myModal').modal('show');
                            $.each(res.data, function (name, value) {
                                //使用数据的拼接,找到数据项,其实上边的console.log(res);来显示数据
                                $("#id_" + name).val(value);
                            })
                        } else {
                            alert(res)
                        }
                    }
                })
            })

        }
    </script>
{% endblock %}